package net.sureon.common.config;

import java.io.FileNotFoundException;
import java.net.URL;
import java.util.List;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class SureonConfig {
    private static final String ERROR_NO_FILE_IN_CLASSPATH = "No such file in classpath: ";
    private static final String CONFIG_FILE_PATH = "/conf/sureonConfig.xml";

    private static final String CONF_SECTION_MAIL = "mail";
    private static final String CONF_SECTION_PROPERTIES = "properties";
    private static final String CONF_SECTION_PROPERTY = "property";
    private static final String CONF_ATTRI_NAME = "name";

    private Properties confProps = new Properties();
    private static SureonConfig instance = new SureonConfig();

    public static final String CONF_NAME_BASE_BRAND_DIR = "brandBaseDir";
    public static SureonConfig getInstance() {
        return instance;
    }

    private MailConfig mailConfig;

    private SureonConfig() {
        Document doc = parseResource(CONFIG_FILE_PATH);
        mailConfig = new MailConfig(doc.getRootElement().element(
            CONF_SECTION_MAIL));

        Element props = doc.getRootElement().element(CONF_SECTION_PROPERTIES);
        List<Element> propElems = props.elements(CONF_SECTION_PROPERTY);
        for (Element propElem : propElems) {
            confProps.setProperty(propElem.attributeValue(CONF_ATTRI_NAME),
                propElem.getTextTrim());
        }
    }

    public String getProperty(String key) {
        return confProps.getProperty(key);
    }

    public String getProperty(String key, String def) {
        return confProps.getProperty(key, def);
    }

    public int getPropertyAsInt(String key) {
        String val = getProperty(key);
        if (val != null) {
            return Integer.parseInt(val);
        }
        return 0;
    }

    public int getPropertyAsInt(String key, int def) {
        String val = getProperty(key);
        if (val != null) {
            return Integer.parseInt(val);
        }
        return def;
    }

    public long getPropertyAsLong(String key) {
        String val = getProperty(key);
        if (val != null) {
            return Long.parseLong(val);
        }
        return 0;
    }

    public long getPropertyAsLong(String key, long def) {
        String val = getProperty(key);
        if (val != null) {
            return Long.parseLong(val);
        }
        return def;
    }

    public MailConfig getMailConfig() {
        return mailConfig;
    }

    /**
     * Parse a resource file into a document
     * 
     * @param resourcePath
     *            Full path of the resource.
     * @return document representation of the file.
     * @throws DocumentException
     * @throws FileNotFoundException
     */
    public static Document parseResource(String resourcePath) {

        /* now attempt to load and parse the file. */
        SAXReader theReader = new SAXReader();
        /**
         * Using getResource instead of getResourceStream as stream seem to be
         * cached by Tomcat.
         */
        URL url = SureonConfig.class.getResource(resourcePath);
        if (url == null) {
            throw new RuntimeException(ERROR_NO_FILE_IN_CLASSPATH
                + resourcePath);
        }
        try {
            return theReader.read(url);
        } catch (DocumentException e) {
            e.printStackTrace();
            throw new RuntimeException("Error during parsing XML documents.");
        }
    }
}
